package com.linkturing.swing;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;


public class LengthInputSwing extends JFrame {
    private JPanel contentPane;
    private JFrame frame;
    private JTextField lengthTextField;
    private JTextField originTextFidld;

    public LengthInputSwing(JTextField originTextFidld) throws HeadlessException {
        frame = this;
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setBounds(700, 300, 400, 200);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        frame.setTitle("请输入长度");

        //密码：
        JLabel lblNewLabel = new JLabel("\u957f\u5ea6");
        lblNewLabel.setBounds(42, 10, 53, 30);
        contentPane.add(lblNewLabel);

        lengthTextField = new JTextField();
        lengthTextField.setBounds(100, 10, 180, 30);
        contentPane.add(lengthTextField);

		/*
		 * lengthTextField.addKeyListener(new KeyAdapter() {
		 * 
		 * @Override public void keyReleased(KeyEvent e) {
		 * if(StrUtil.isBlank(lengthTextField.getText())) {
		 * lengthTextField.setText("请输入长度"); } } });
		 */

        //确定
        JButton btnNewButton = new JButton("\u786E\u5B9A");
        btnNewButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(StrUtil.isBlank(lengthTextField.getText())) {
                    JOptionPane.showMessageDialog(frame, "请输入长度！");
                    return;
                }
                if(!Validator.isNumber(lengthTextField.getText())) {
        			JOptionPane.showMessageDialog(frame, "分卷號必須是數字");
        			return;
        		}
                originTextFidld.setText(lengthTextField.getText());
                frame.setVisible(false);
                frame.dispose();
                XinhuaApplication.weight();
            }
        });

        contentPane.add(btnNewButton);
        btnNewButton.setBounds(200, 100, 80, 30);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    LengthInputSwing frame = new LengthInputSwing(null);
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
